package com.example.dmallOrder.common.dmall.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.Map;

/**
 * todo
 *
 * @author:yanbo.li
 * @date: 2023/5/23
 */
public class MyHttpRequest {


    /**
     * 发送http 请求
     * @param url
     * @param appSecret
     * @param treeMapP
     * @param headermap
     * @return
     */
    public static  String sendRequest(String url, String appSecret,Map<String,Object> treeMapP,Map<String, String> headermap ) throws Exception {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        Map result = null;
        try {
            httpClient = HttpClients.createDefault();
            HttpPost httpPost= HttpUtil.getHttpPost(url,treeMapP,"UTF-8",headermap);
            response= httpClient.execute(httpPost);
            HttpEntity valueEntity = response.getEntity();
            String content = EntityUtils.toString(valueEntity);
            System.out.println("content = " + content);
            String dataStr= EncryDecryptUtil.decrypt(content,appSecret);
            return  dataStr;
        }
        catch (Exception ex){
            System.out.println("ex = " + ex);
            throw  ex;
        }
    }


}
